import 'package:basic_common/app_theme.dart';
import 'package:basic_common/common_exports.dart';
import 'package:basic_widgets/app_bar_widget.dart';
import 'package:basic_widgets/image_widget.dart';
import 'package:chat_bubbles/chat_bubbles.dart';
import '../modle/chat_data.dart';
import 'chat_controller.dart';

class ChatPage extends GetView<ChatController> {
  ChatPage({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: backAppbar(title: "聊天"),
      body: Column(children: [
        Expanded(child: _buildChatList()),
        _buildInputBox(),
      ]),
    );
  }

  /// 创建聊天信息
  Widget _buildChatList() {
    return Padding(
      padding: EdgeInsets.symmetric(
          horizontal: ScreenHelper.width(15),
          vertical: ScreenHelper.height(10)),
      child: GetBuilder<ChatController>(
        id: 'updateMsg',
        builder: (controller) => ListView.builder(
          shrinkWrap: true,
          reverse: true,
          itemBuilder: (context, index) {
            Chatdata chatData = controller.chatList.reversed.toList()[index];
            return chatData.isSender!
                ? Column(
                    children: [
                      Opacity(
                        opacity: chatData!.isShowTime! ? 1 : 0,
                        child: Text(
                          "${chatData.time!}",
                          style: TextStyle(
                              color: AppTheme.weakTextColor,
                              fontSize: ScreenHelper.sp(12)),
                        ),
                      ),
                      // Text("${chatData.isShowTime}"),
                      chatItemSender(
                          text: chatData.text!,
                          isSender: chatData.isSender!,
                          type: chatData.type),
                    ],
                  )
                : Column(
                    children: [
                      Opacity(
                        opacity: chatData!.isShowTime! ? 1 : 0,
                        child: Text(
                          "${chatData!.time!}",
                          style: TextStyle(
                              color: AppTheme.weakTextColor,
                              fontSize: ScreenHelper.sp(12)),
                        ),
                      ),
                      chatItem(
                          text: chatData.text!,
                          isSender: chatData.isSender!,
                          type: chatData.type),
                    ],
                  );
          },
          itemCount: controller.chatList.value.length,
        ),
      ),
    );
  }

  /// 接收方
  Widget chatItem(
      {required String text, bool isSender = true, required int type}) {
    return Row(
      children: [
        circleImg(head: controller.pPhotoUrl.value),
        type == 1
            ? BubbleNormal(
                color: isSender ? AppTheme.primyColor : Colors.white,
                tail: true,
                delivered: false,
                text: text,
                isSender: isSender,
                textStyle: TextStyle(
                    color: isSender ? Colors.white : AppTheme.dartTextColor,
                    fontSize: ScreenHelper.sp(14)),
              )
            : BubbleNormalImage(
                id: '01011',
                image: netWorkImgBox(netPath: text),
                isSender: isSender,
                tail: true,
                color: isSender ? AppTheme.primyColor : Colors.white,
                delivered: false,
              ),
      ],
    );
  }

  /// 发送方
  Widget chatItemSender(
      {required String text, bool isSender = true, required int type}) {
    return Row(
      children: [
        type == 1
            ? Expanded(
                child: BubbleNormal(
                  color: isSender ? AppTheme.primyColor : Colors.white,
                  tail: true,
                  delivered: false,
                  text: text,
                  constraints:
                      BoxConstraints(maxWidth: ScreenHelper.screenWidth),
                  isSender: isSender,
                  textStyle: TextStyle(
                      color: isSender ? Colors.white : AppTheme.dartTextColor,
                      fontSize: ScreenHelper.sp(14)),
                ),
              )
            : Expanded(
                child: BubbleNormalImage(
                id: '01010',
                image: netWorkImgBox(netPath: text),
                isSender: isSender,
                tail: true,
                color: isSender ? AppTheme.primyColor : Colors.white,
                delivered: false,
              )),
        circleImg(head: controller.dPhotoUrl.value)
      ],
    );
  }

  Widget _buildInputBox() {
    return Column(
      children: [
        Container(
          alignment: Alignment.bottomCenter,
          width: ScreenHelper.screenWidth,
          height: ScreenHelper.height(50),
          color: Colors.white,
          padding: EdgeInsets.symmetric(vertical: 4, horizontal: 12),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              // Obx(
              //   () => controller.isShowInput.value
              //       ? Opacity(
              //         opacity: 0,
              //         child: IconButton(
              //             icon: Icon(
              //               Icons.mic,
              //             ),
              //             iconSize: ScreenHelper.width(32),
              //             color: AppTheme.dartWeakTextColor,
              //             onPressed: () {
              //               controller.switchVoice();
              //             },
              //           ),
              //       )
              //       : GestureDetector(
              //           onTap: () {
              //             controller.isShowInput.value = true;
              //           },
              //           child: Container(
              //             margin: EdgeInsets.only(right: ScreenHelper.width(10)),
              //             child: Image.asset(
              //               "assets/message/icon_keybord.png",
              //               width: ScreenHelper.width(32),
              //               height: ScreenHelper.width(32),
              //             ),
              //           ),
              //         ),
              // ),
              Obx(() => controller.isShowInput.value
                      ? Expanded(
                          child: Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(50),
                            ),
                            child: Container(
                              margin: const EdgeInsets.symmetric(horizontal: 6),
                              child: TextField(
                                cursorColor: AppTheme.dartTextColor,
                                onChanged: (value) {},
                                onSubmitted: (value) {
                                  controller.sendMsgToP(msgType: '1');
                                },
                                style: const TextStyle(
                                    color: AppTheme.dartTextColor),
                                controller: controller.inputController,
                                autofocus: false,
                                decoration: InputDecoration(
                                  contentPadding: EdgeInsets.symmetric(
                                      vertical: 4.0,
                                      horizontal: ScreenHelper.width(10)),
                                  hintText: "请输入聊天内容",
                                  // contentPadding: EdgeInsets.all(10),
                                  border: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(50),
                                      borderSide: BorderSide.none),
                                  filled: true,
                                  fillColor: AppTheme.appBgColor,
                                  hintStyle:
                                      TextStyle(fontSize: ScreenHelper.sp(13)),
                                ),
                                textInputAction: TextInputAction.send,
                              ),
                            ),
                          ),
                        )
                      : SizedBox.shrink()
                  //  Expanded(
                  //     child: VoiceWidget(
                  //     startRecord: () {
                  //       print("开始录音");
                  //     },
                  //     stopRecord: () {
                  //       print("结束录音");
                  //     },
                  //   )),
                  ),
              // IconButton(
              //   icon: Icon(Icons.emoji_emotions_outlined),
              //   color: AppTheme.weakTextColor,
              //   iconSize: ScreenHelper.width(32),
              //   onPressed: () {},
              // ),
              SizedBox(
                width: ScreenHelper.width(10),
              ),
              IconButton(
                icon: Icon(Icons.image),
                color: AppTheme.weakTextColor,
                iconSize: ScreenHelper.width(32),
                onPressed: () {
                  controller.selectImg();
                },
              )
            ],
          ),
        ),
        Obx(() =>
            controller.isShowBottom.value ? buildBottom() : SizedBox.shrink())
      ],
    );
  }

// 底部选择图片
  Widget buildBottom() {
    return Container(
      width: ScreenHelper.screenWidth,
      height: ScreenHelper.height(140),
      padding: EdgeInsets.all(ScreenHelper.width(10)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          GestureDetector(
              onTap: () => controller.showSelectImg(1),
              child: buildItem(
                  icon: 'assets/message/chat/img_camera.png', text: '拍照')),
     
          GestureDetector(
              onTap: () => controller.showSelectImg(2),
              child: buildItem(
                  icon: 'assets/message/chat/img_picker.png', text: '相册')),
          GestureDetector(
              onTap: () => controller.navToPresc(),
              child: buildItem(
                  icon: 'assets/message/chat/img_chufang.png', text: '开处方')),
          // Obx(() => Text("图片==${controller.selImgPath.value}"))
        ],
      ),
    );
  }

  Widget buildItem({required String icon, required String text}) {
    return Container(
      width: ScreenHelper.width(90),
      height: ScreenHelper.width(90),
      padding: EdgeInsets.symmetric(vertical: 4),
      alignment: Alignment.center,
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(10)),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            icon,
            width: ScreenHelper.width(48),
            height: ScreenHelper.width(48),
            fit: BoxFit.fill,
            package: "basic_common",
          ),
          SizedBox(
            height: ScreenHelper.height(10),
          ),
          Text(
            text,
            style: TextStyle(
                color: AppTheme.dartTextColor, fontSize: ScreenHelper.sp(14)),
          )
        ],
      ),
    );
  }
}
